/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PainelEndereco.java
 *
 * Created on 03/07/2011, 18:49:19
 */
package manager.principal.fonte;

import java.awt.event.KeyEvent;
import java.util.LinkedList;
import manager.auxiliares.AutoComplete;
import manager.auxiliares.Configurations;
import manager.auxiliares.MensagemExcessao;
import manager.auxiliares.Format;
import manager.helpers.validation.TextFieldValidator;
import manager.principal.dao.EnderecoDao;
import manager.principal.dao.CidadeDao;
import manager.principal.dao.EstadoDao;

/**
 *
 * @author Thiago
 */
public class PainelEndereco extends javax.swing.JPanel {

    private JanelaCidade cityWindow;
    private AutoComplete autoCompleteCity;

    /**
     * Creates new form PainelEndereco
     */
    public PainelEndereco() {
        initComponents();
        autoCompleteCity = new AutoComplete(jList1, jScrollPane1, jTextField3);
        setEditable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    public LinkedList<String> validarCampos() {
        LinkedList<String> fieldList = new LinkedList<String>();
        if (jCheckBox1.isSelected()) {
            if (CidadeDao.getIdCidadePorNome(jTextField3.getText()) == 0) {
                fieldList.add(((TextFieldValidator) jTextField3).getjLabel().getText());
            }
        }
        return fieldList;
    }

    public void carregarInformacoes(int idEndereco) {
        EnderecoDao enderecoDao = new EnderecoDao();
        enderecoDao.buscar(idEndereco);

        jCheckBox1.setSelected(true);

        CidadeDao cidadeDao = new CidadeDao();
        cidadeDao.buscar(enderecoDao.getIdCidade());
        jTextField3.setText(cidadeDao.getNome());
        jTextField7.setText(EstadoDao.getNomeEstadoPorId(cidadeDao.getIdEstado()));

        jTextField6.setText(enderecoDao.getNumero());
        jTextField4.setText(enderecoDao.getCep());
        jTextField1.setText(enderecoDao.getEndereco());
        jTextField2.setText(enderecoDao.getBairro());
        jTextField5.setText(enderecoDao.getComplemento());

        validarCampos();

    }

    public EnderecoDao getEndereco() {
        EnderecoDao addressDao = new EnderecoDao();
        addressDao.setEndereco(jTextField1.getText());
        addressDao.setBairro(jTextField2.getText());
        addressDao.setNumero(jTextField6.getText());
        addressDao.setCep(jTextField4.getText());
        addressDao.setComplemento(jTextField5.getText());
        addressDao.setIdCidade(CidadeDao.getIdCidadePorNome(jTextField3.getText()));

        return addressDao;
    }

    private void setEditable(boolean isEnable) {
        jTextField1.setEnabled(isEnable);
        jTextField2.setEnabled(isEnable);
        jTextField3.setEnabled(isEnable);
        jTextField4.setEnabled(isEnable);
        jTextField5.setEnabled(isEnable);
        jTextField6.setEnabled(isEnable);

        jButton1.setEnabled(isEnable);
        if (isEnable) {
            jLabel3.setForeground(Configurations.getFieldValidationNotOk());
        } else {
            jLabel3.setForeground(Configurations.getFieldValidationOk());
            jScrollPane1.setVisible(false);
            jTextField3.setText("");
            jTextField7.setText("");
        }
    }

    private void checkCity() {
        if (jCheckBox1.isSelected()) {
        } else {
            jLabel3.setForeground(Configurations.getFieldValidationOk());
        }
    }

    private void selecionarCidade() {
        int idCidade = CidadeDao.getIdCidadePorNome(jTextField3.getText());
        if (idCidade != 0) {
            CidadeDao cidadeDao = new CidadeDao();
            cidadeDao.buscar(idCidade);
            jTextField7.setText(EstadoDao.getNomeEstadoPorId(cidadeDao.getIdEstado()));
            autoCompleteCity.getjScrollPane().setVisible(false);
            jTextField6.requestFocus();
        } else {
            jTextField7.setText("");
        }

    }

    public boolean estaSelecionado() {
        return jCheckBox1.isSelected();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new TextFieldValidator(80, -1, false, true, "text", jLabel2);
        jTextField3 = new TextFieldValidator(80, -1, true, true, "text", jLabel3);
        jTextField4 = new TextFieldValidator(8, -1, false, true, "text", jLabel4);
        jTextField5 = new TextFieldValidator(-1, -1, false, true, "text", jLabel6);
        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField1 = new TextFieldValidator(80, -1, false, true, "text", jLabel1);
        jTextField6 = new TextFieldValidator(10, -1, false, true, "text", jLabel5);
        jLabel41 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();

        setBackground(Configurations.getBackGroundColor());
        setLayout(null);

        jScrollPane1.setVisible(false);

        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jList1MouseReleased(evt);
            }
        });
        jList1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jList1KeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        add(jScrollPane1);
        jScrollPane1.setBounds(50, 50, 220, 50);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Endereço");
        add(jLabel1);
        jLabel1.setBounds(50, 55, 90, 14);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Bairro");
        add(jLabel2);
        jLabel2.setBounds(325, 55, 90, 14);

        jLabel3.setBackground(Configurations.getBackGroundColor());
        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 255));
        jLabel3.setText("Cidade");
        add(jLabel3);
        jLabel3.setBounds(50, 10, 90, 14);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Cep");
        add(jLabel4);
        jLabel4.setBounds(415, 10, 34, 14);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Número");
        add(jLabel5);
        jLabel5.setBounds(355, 10, 50, 14);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setText("Complemento");
        add(jLabel6);
        jLabel6.setBounds(50, 100, 100, 14);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/manager/images/search.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(278, 28, 30, 23);
        add(jTextField2);
        jTextField2.setBounds(325, 75, 165, 20);

        jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField3KeyReleased(evt);
            }
        });
        add(jTextField3);
        jTextField3.setBounds(50, 30, 220, 20);
        add(jTextField4);
        jTextField4.setBounds(415, 30, 75, 20);
        add(jTextField5);
        jTextField5.setBounds(50, 120, 440, 20);

        jCheckBox1.setBackground(Configurations.getBackGroundColor());
        jCheckBox1.setFocusable(false);
        jCheckBox1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jCheckBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBox1ItemStateChanged(evt);
            }
        });
        add(jCheckBox1);
        jCheckBox1.setBounds(1, 10, 47, 130);
        add(jTextField1);
        jTextField1.setBounds(50, 75, 270, 20);
        add(jTextField6);
        jTextField6.setBounds(355, 30, 55, 20);

        jLabel41.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel41.setText("UF");
        add(jLabel41);
        jLabel41.setBounds(315, 10, 14, 14);

        jTextField7.setEditable(false);
        jTextField7.setFocusable(false);
        add(jTextField7);
        jTextField7.setBounds(310, 30, 40, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (cityWindow != null) {
            cityWindow.dispose();
        }
        cityWindow = new JanelaCidade();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jCheckBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBox1ItemStateChanged
        setEditable(jCheckBox1.isSelected());
    }//GEN-LAST:event_jCheckBox1ItemStateChanged

    private void jTextField3KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField3KeyReleased
        try {
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                selecionarCidade();
            } else if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
                if (autoCompleteCity.getjScrollPane().isVisible()) {
                    autoCompleteCity.getjList().setSelectedIndex(0);
                    autoCompleteCity.getjList().requestFocus();
                }
            } else {
                if (!autoCompleteCity.getSearchWord().equals(jTextField3.getText())) {
                    autoCompleteCity.setSearchWord(jTextField3.getText());
                    autoCompleteCity.update(new CidadeDao().buscar("Nome", jTextField3.getText(), true));
                }
            }
        } catch (Exception e) {
            MensagemExcessao.mostrarMensagem(e, "Endereços");
        }
    }//GEN-LAST:event_jTextField3KeyReleased

    private void jList1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jList1KeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            autoCompleteCity.setSearchWord(autoCompleteCity.getjList().getSelectedValue().toString());
            selecionarCidade();
        }
    }//GEN-LAST:event_jList1KeyReleased

    private void jList1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseReleased
        autoCompleteCity.setSearchWord(autoCompleteCity.getjList().getSelectedValue().toString());
        selecionarCidade();
    }//GEN-LAST:event_jList1MouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel41;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    // End of variables declaration//GEN-END:variables
}
